package com.linkdood.app.web.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linkdood.app.dto.MessageDetailDTO;
import com.linkdood.app.dto.NoticeMessageDTO;
import com.linkdood.app.exception.InvalidParamException;
import com.linkdood.app.service.AccountService;
import com.linkdood.app.service.NoticeMessageService;
import com.linkdood.app.utils.PaginationUtil;
import com.linkdood.app.utils.ResourceUtil;
import com.linkdood.app.vm.AccountVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 消息通知接口
 *
 * @author YunJ
 */
@Api(value = "消息通知接口", tags = {"消息通知接口"})
@Slf4j
@RestController
@RequestMapping("/api/messages")
public class MessageResource {

    private final NoticeMessageService messageService;
    private final AccountService accountService;

    public MessageResource(NoticeMessageService messageService, AccountService accountService) {
        this.messageService = messageService;
        this.accountService = accountService;
    }

    @PreAuthorize("@ss.hasPermi('bxy:messages:ordinary','bxy:messages:template')")
    @ApiOperation(value = "新增通知")
    @PostMapping("/dood")
    public ResponseEntity<?> createNotice(@Valid @RequestBody NoticeMessageDTO noticeDTO, BindingResult result) {
        if (result.hasErrors()) {
            throw InvalidParamException.i18nException(result.getFieldError());
        }
        AccountVM user = accountService.getCurrentAccountInfo();
        List<MessageDetailDTO> messageDetailDTOS = messageService.createNotice(noticeDTO, user);
        return ResponseEntity.ok(messageDetailDTOS);
    }

    @PreAuthorize("@ss.hasPermi('bxy:messages:details')")
    @ApiOperation(value = "获取通知详情")
    @GetMapping("/{id}")
    public ResponseEntity<?> getNotice(@PathVariable String id) {
        MessageDetailDTO detailDTO = messageService.getMessageDetail(id);
        return ResponseEntity.ok(detailDTO);
    }

    /**
     * 查询通知列表
     *
     * @param title     标题
     * @param content   内容
     * @param startTime 开始时间 YYYY-MM-DDTHH:MM:SSZ
     * @param endTime   结束时间 YYYY-MM-DDTHH:MM:SSZ
     * @param pageable  分页
     */
    @PreAuthorize("@ss.hasPermi('bxy:messages:index')")
    @GetMapping
    public ResponseEntity<?> getNotices(@RequestParam(value = "title", required = false) String title,
                                        @RequestParam(value = "content", required = false) String content,
                                        @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
                                        @RequestParam(value = "endTime", required = false) LocalDateTime endTime,
                                        Pageable pageable) {

        Page<MessageDetailDTO> page = messageService.pageNotices(title, content, startTime, endTime, PaginationUtil.getPage(pageable));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page);
        return new ResponseEntity<>(page.getRecords(), headers, HttpStatus.OK);
    }

    /**
     * 删除通知
     * @param id 通知id
     */
    @PreAuthorize("@ss.hasPermi('bxy:messages:delete')")
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteNotices(@PathVariable String id) {
        messageService.removeById(id);
        return ResponseEntity.ok(ResourceUtil.success());
    }
}
